package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.POIUtils;
import cn.itcast.vo.OrderSettingVo;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author FH
 * @createTime 2021年11月15日 14:54:00
 */
@RestController
@RequestMapping(path = "/ordersetting")
@CrossOrigin
@Slf4j
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 预约设置进入查询
     *
     * @return
     */
    @GetMapping("/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(@RequestParam("date") String orderDate) {
        log.info("----预约设置进入查询:{}", orderDate);
        try {
            List<OrderSettingVo> result = this.orderSettingService.getOrderSettingsByMonth(orderDate);
            log.info("预约设置进入查询的结果:{}", result);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 单日预约设置
     *
     * @param params
     * @return
     */
    @PutMapping("/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody Map<String, Object> params) {
        log.info("单日预约设置:{}", params);
        try {
            Integer number = Integer.parseInt((String) params.get("number"));
            Date orderDate = DateUtils.parseString2Date((String) params.get("orderDate"));
            Boolean result = this.orderSettingService.updateOrderSettingNumber(orderDate, number);
            if (result) {
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ORDERSETTING_FAIL);
    }

    /**
     * 上传文件(批量设置预约数)
     *
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        log.info("上传文件:{}",file);
        try {
            List<String[]> readExcelList = POIUtils.readExcel(file);
            for (String[] strings : readExcelList) {
                System.out.println("日期:"+strings[0]);
            }
            log.info("上传文件解析后的结果:{}",readExcelList);
            this.orderSettingService.upload(readExcelList);
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.UPLOAD_FAIL);
    }
}
